<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Tabcontroller extends CI_Controller {

	var $orgid;
	
	function __construct()
	{
		parent::__construct();
	
		$this->orgid=$this->tank_auth->get_user_id();
	
	}

	public function webservicestab(){
		$this->load->view('webservices');
	}

	public function customwebservicestab(){
		$this->load->view('customwebservices');
	}

	public function defaultwebservices(){
		$this->load->view('defaultwebservices');
	}

	public function profiletab(){
		$this->load->view('profile');
	}

	public function welcometab(){
		$this->load->view('welcome');
	}

	public function adddatatab(){
		$this->load->view('adddata');
	}

	public function viewdatatab(){
		$this->load->view('viewdata');
	}

	public function addcustomwebservice(){

		$orgid=$this->orgid;
		
		$data=array(
			'OrgID'=>$this->orgid,
			'WSDL'=>$this->input->post("webservice-wsdl",TRUE),
			'descr'=>$this->input->post("webservice-name",TRUE),
			'MethodName'=>$this->input->post("webservice-operation",TRUE),
		);

		$this->db->insert('service',$data);

		echo $this->db->affected_rows();
	}

	public function fetchparamsfromwebservice(){

		$wsdl=$this->input->post("wsdl",TRUE);
		$operation=$this->input->post("operation",TRUE);

		$client=new SoapClient($wsdl);
		$types=$client->__getTypes();
		$paramdata=array();

		foreach($types as $type){
			//echo strchr($type,"CreditCard");
			if(strstr($type,$operation)!= ""){
				$pos= stripos($type,'{');
				if(strstr($type,"Response")!= ""){
					$json_string=str_replace("{","",substr($type,$pos));
					$json_string=str_replace("}","",$json_string);
					$json_string=substr($json_string,0,strlen($json_string)-2);
					$params = preg_split("/[;]/", $json_string);
					$data=array();
					foreach($params as $param){
						$paramArray = preg_split("/[\s]+/", trim($param));
						$data[$paramArray[1]]=$paramArray[0];
					}
					$paramdata['output']=$data;

				}else{
					$json_string=str_replace("{","",substr($type,$pos));
					$json_string=str_replace("}","",$json_string);
					$json_string=substr($json_string,0,strlen($json_string)-2);
					$params = preg_split("/[;]/", $json_string);
					$data=array();
					foreach($params as $param){
						$paramArray = preg_split("/[\s]+/", trim($param));
						$data[$paramArray[1]]=$paramArray[0];
					}
					$paramdata['input']=$data;
				}
			}
		}

		echo json_encode($paramdata);
	}

	public function createworkflow(){

		$orgid=$this->orgid;
		$name=$this->input->post("wfname",TRUE);

		$data=array("OrgID"=>$this->orgid,"name"=>$name,"ActiveInd"=>1);
		//
		$this->db->insert('workflow',$data);
		 
		$insertid=$this->db->insert_id();
		 
		 
		$sql="INSERT INTO `workflowservicemapping` (`OrgId`, `workflow_id`, `service_id`, `screen_position`, `screen_order`) VALUES
			( '{$orgid}', '{$insertid}', 1, 1, 1),
			( '{$orgid}', '{$insertid}', 2, 2, 2),
			( '{$orgid}', '{$insertid}', 3, 0, 1),
			( '{$orgid}', '{$insertid}', 4, 3, 1),
			( '{$orgid}', '{$insertid}', 5, 4, 1),
			( '{$orgid}', '{$insertid}', 6, 3, 2),
			( '{$orgid}', '{$insertid}', 7, 4, 2),
			( '{$orgid}', '{$insertid}', 8, 5, 1),
			( '{$orgid}', '{$insertid}', 9, 6, 1),
			( '{$orgid}', '{$insertid}', 10, 2, 1);";
		
		$this->db->query($sql);

// 		
		$sql2="INSERT INTO `workflowservicemapping` (`OrgId`, `workflow_id`, `service_id`, `screen_position`, `screen_order`) VALUES";
		
		$query=$this->db->query("select ServiceID from service where OrgID =".$orgid);
		foreach($query->result() as $row){
			
			$sql2=$sql2."( '{$orgid}', '{$insertid}', '{$row->ServiceID}', 0, 1),";
		}
		$sqlstring=substr($sql2,0,strlen($sql2)-1).";";
		$this->db->query($sqlstring);
		
		echo $sqlstring;
		echo "SUCCESS";
	}
	
	public function makeThisActive(){
	
		$orgid=$this->orgid;
		$wfid=$this->input->post("wfid",TRUE);
		
		$queryOne="update `workflow` set ActiveInd=0 where OrgID=".$orgid;
		$queryTwo="update `workflow` set ActiveInd=1 where OrgID=".$orgid." and WorkflowID=".$wfid;
	
		$this->db->query($queryOne);
		$this->db->query($queryTwo);
		
		echo "Success";
	}
	
	public function getActiveWorkflow(){
	
		$orgid=$this->orgid;
	
		$queryOne="select name from  `workflow` where ActiveInd=1 and OrgID=".$orgid;
		$result=$this->db->query($queryOne);
		
		
		if ($result->num_rows() > 0)
		{
			foreach ($result->result() as $row)
			{
				echo $row->name;
			}
		}else{
			echo 'Default Workflow 001';
		}
	
	}
	
	public function getcustomwebservices(){
		$orgid=$this->orgid;
		$this->db->select('*');
		$this->db->from('service');
		$this->db->where('OrgID',$orgid);
		$query=$this->db->get();
		
		$data=array();
		
		foreach($query->result() as $row){
			
			$data[]=array(
			"serviceid"=> $row->ServiceID,
			"descr"=>$row->descr,
			"wsdl"=>$row->WSDL,
			"method"=>$row->MethodName
			);
		
		}
		
		echo json_encode($data);
	}
	
	public function updatecustomwebservice(){
		

		$serviceid=$this->input->post("serviceid",TRUE);
		$descr=$this->input->post("descr",TRUE);
		$wsdl=$this->input->post("wsdl",TRUE);
		$method=$this->input->post("method",TRUE);

		$data=array("descr"=>$descr,"WSDL"=>$wsdl,"MethodName"=>$method);
		
		$this->db->where('ServiceID', $serviceid);
		$this->db->update('service', $data);
		
		echo "SUCCESS";
	}
	
	public function deletecustomwebservice(){
	
	}
	
	public function workflowtestdisp(){
		$wfid=$this->input->post("wfid",TRUE);
		$dataobj=$this->input->post("wfid",TRUE);
		
		$this->load->model('workflowmodel','workflow');
		
		$wfarray=$this->workflow->getworkflowarray($wfid);
		
		echo '<div style="padding:10px; margin: 5px; border:thin solid #000;">';
			
		foreach($wfarray['service_array'] as $service){
			
			$this->workflow->fetchhtmlfromservice($service['descr']);
			echo '</br>';
		}
		
		echo '</div>';
		
		
	}
}